package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.BannerDaoImpl;
import com.ysd.entity.Banner;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

/**
 * 显示轮播图
 * localhost:8080/shangcheng1/adminBanner?reqName=selAdminBanner
 * @author Administrator
 *
 */
public class AdminBannerServlet extends BaseServlet {
	
	public void selAdminBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//创建实现类对象
		BannerDaoImpl bDao = new BannerDaoImpl();
		
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<Banner>bList = bDao.getBannerByPage(page, size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(bList);
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * localhost:8080/shangcheng1/adminBanner?reqName=selAllAdminBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
public void selAllAdminBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//创建实现类对象
		BannerDaoImpl bDao = new BannerDaoImpl();
		
		
		List<Banner>bList = bDao.getBanner();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(bList.size());
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据id删除轮播图
	 * @param localhost:8080/shangcheng1/adminBanner?reqName=delAdminBanner
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delAdminBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		int banId = Integer.parseInt(request.getParameter("bannerId"));
		BannerDaoImpl banDao = new BannerDaoImpl();//创建实现类对象
		int n = banDao.delAdminBanner(banId);//调用实现类中的删除方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if(n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
	}

	/**
	 * localhost:8080/shangcheng1/adminBanner?reqName=addBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		BannerDaoImpl banDao = new BannerDaoImpl();
		
		String banberImg = request.getParameter("bannerImg");
		Banner ba = new Banner(0, banberImg);
		System.out.println(ba);
		int n = banDao.addBanner(ba);
			
		if (n > 0) {
			out.print(Result.toClient(true, "添加成功", ""));
		} else {
			out.print(Result.toClient(false, "添加失败", ""));
		}
	
	}
	
	/**
	 * localhost:8080/shangcheng1/adminBanner?reqName=getBannerById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getBannerById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		BannerDaoImpl banDao = new BannerDaoImpl();
		
		int banId = Integer.parseInt(request.getParameter("bannerId"));
		Banner ban = banDao.selBanner(banId);
		
		Gson gson = new Gson();
		String res = gson.toJson(ban);
		out.print(res);
		
		out.flush();
		out.close();
	
	}
	
	/**
	 * localhost:8080/shangcheng1/adminBanner?reqName=updateBannerById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateBannerById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		BannerDaoImpl banDao = new BannerDaoImpl();
		
		int bannerId = Integer.parseInt(request.getParameter("bannerId"));
		String bannerImg = request.getParameter("bannerImg");
		Banner ban = new Banner(bannerId, bannerImg);
		int n = banDao.updateBanById(ban);
		
		if (n > 0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		
		out.flush();
		out.close();
	
	}
	
	/**
	 * 根据多条轮播图id删除产品
	 * localhost:8080/shangcheng1/adminBanner?reqName=delBannerByIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delBannerByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		BannerDaoImpl dDao = new BannerDaoImpl();
		String ids = request.getParameter("ids");
		int n = dDao.delBannerByIds(ids);
		
				
			if (n > 0) {
				out.print(Result.toClient(true, "删除成功", ""));
			} else {
				out.print(Result.toClient(false, "删除失败", ""));
			}
			
			out.flush();
			out.close();

	}
	
}
